package com.ruoyi.heima.service.impl;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.heima.domain.po.NewsType;
import com.ruoyi.heima.mapper.NewsTypeMapper;
import com.ruoyi.heima.service.INewsTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * newsService业务层处理
 * 
 * @author weiyi
 * @date 2025-06-21
 */
@Service
public class NewsTypeServiceImpl extends ServiceImpl<NewsTypeMapper, NewsType> implements INewsTypeService {
    @Autowired
    private NewsTypeMapper newsTypeMapper;


    public NewsType getById(Long typeId) {
        return newsTypeMapper.selectById(typeId);
    }

    /**
     * 分页查询news列表
     * @return news
     */
    public TableDataInfo pageNewsType(Integer pageNum, Integer pageSize,String typeName) {
        Page<NewsType> page = lambdaQuery().eq(StringUtil.isNotBlank(typeName), NewsType::getTypeName, typeName).
                page(new Page<>(pageNum, pageSize));
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }
    /**
     * 新增news
     *
     * @param newsType news
     * @return 结果
     */
    public int insertNewsType(NewsType newsType) {
        newsType.setCreateTime(DateUtils.getNowDate());
        return newsTypeMapper.insertNewsType(newsType);
    }
    /**
     * 修改news
     *
     * @param newsType news
     * @return 结果
     */
    @Override
    public int updateNewsType(NewsType newsType) {
        newsType.setUpdateTime(DateUtils.getNowDate());
        return newsTypeMapper.updateNewsType(newsType);

   }
   /**
     * 批量删除news
     *
     * @param typeIds 需要删除的news主键
     * @return 结果
     */
    @Override
    public int deleteNewsTypeByTypeIds(Long[] typeIds) {
        //检查是否有子类型
        Integer newsTypes = newsTypeMapper.getAllNewsTypes(typeIds);
        if (newsTypes > 0){
            throw new RuntimeException("请先删除子类型");
        }
        return newsTypeMapper.deleteNewsTypeByTypeIds(typeIds);
    }


}













}







